<?php
require_once('IndexController.php');

class Front_CategoryController extends Front_IndexController
{
    public function init()
    {
    	
		parent::init();
		try{
	    	$dirTemplate = TEMPLATE_PATH;
	    	$this->createTemplate($dirTemplate );
	    	
    	}catch(Exception $ex){
    		echo "error: ".$ex->getMessage();
    		die;
    	}
    }
    
    public function indexAction(){
    	try{
	    	$categoryId = (int)$this->_request->getParam('id',0);
	    	
	    	if($categoryId >=0){
	    		$pageCategoryTable = new Front_Model_DbTable_PageCategory();
	    		
				//Get all page inside this category
	    		$pageList = $pageCategoryTable->getPageChildren($categoryId, $this->defaultLanguageId);
	    		$this->view->pageList = $pageList;
	    		
	    		$categoryTable = new Front_Model_DbTable_Category();
	    		$categoryName = $categoryTable->getCategoryName($categoryId, $this->defaultLanguageId);
	    		$this->view->categoryName = $categoryName[0]['name'];
	    		
	    	}else{
	    		echo "Your link is not correct, please check again or contact website's admin";
	    	}
    	}catch(Exception $ex){
    		echo $ex->getMessage();
    		exit;
    	}
    }

    public function pageAction(){
    	try{
	    	$id = $this->getRequest()->getParam('id',0);
	    	$id = (int)$id;
	    	if($id > 0){
	    		$pageTable = new Front_Model_DbTable_Page();
	    		
	    		$this->view->newsDetail = $pageTable->getPagesDetail($id);
	    		
	    		//Tăng viewcount
	    		echo $pageTable->increaseViewcounts($id);
	    		
	    	}
	    	else{
	    		echo 'This page does not exists';
	    			
	    	}
    	}catch(Exception $ex){
    		echo $ex->getMessage();
    	}
    }
}